format PE console
entry start
include "%finc%/win32/win32a.inc"
include "%finc%/libs/strlib.inc"
include "bits.inc"
include "lzss.inc"
include "lzssstat.inc"

..ShowSkipped = 0

section '.code' code readable executable

include "%finc%/libs/strlib.asm"
include "bits.asm"
include "cmdln.asm"
include "lzss.asm"
include "lzssstat.asm"


  start:
        invoke  GetCommandLine
        stdcall arrange_cmdline, eax,szDefExt

        mov     ax,'-u'
        call    get_switch_w
        or      eax,eax
        jz      pack_file


     unpack_file:
        cmp     [cmdln.input_fname],0
        je      usage
        stdcall replace_ext, [cmdln.output_fname],szDefOutExt
        stdcall lzssLoadFile,[cmdln.input_fname],unp_size
        push    eax
        invoke  CreateFile, [cmdln.output_fname],GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0
        mov     ebx,eax
        pop     eax
        invoke  WriteFile,ebx,eax,[unp_size],tmp,0
        invoke  CloseHandle,ebx
        stdcall lzssFreeData, esi
        jmp     finish

     pack_file:
        cmp     [cmdln.input_fname],0
        je      usage
        invoke  CreateFile, [cmdln.input_fname],GENERIC_READ,0,0,OPEN_EXISTING,0,0
        cmp     eax,INVALID_HANDLE_VALUE
        je      finish
        mov     ebx,eax
        invoke  SetFilePointer, ebx,0,0,FILE_END
        mov     [input_size],eax
        mov     esi,eax
        invoke  GetProcessHeap
        invoke  HeapAlloc, eax,HEAP_ZERO_MEMORY,esi
        or      eax,eax
        jz      finish
        mov     [input_buf],eax
        invoke  SetFilePointer, ebx,0,0,FILE_BEGIN
        invoke  ReadFile, ebx,[input_buf],esi,tmp,0
        invoke  CloseHandle, ebx
        mov     [lzss.packer_func],lzssStatPack
        mov     ebx,[cmdln.argv]
        stdcall lzssSaveFile, [cmdln.output_fname],[input_buf],[input_size]
        cinvoke printf, szStatus, [lzss_stat.time],[lzss_stat.kbytes_per_sec],\
                                  [lzss_stat.size],[lzss_stat.packed_size],[lzss_stat.ratio],\
                                  [lzss_stat.total_items],[lzss_stat.direct_bytes],\
                                  [lzss_stat.dictionary_entries]
        jmp     finish

     usage:
        cinvoke printf, szUsage
     finish:
        invoke  ExitProcess, 0



iglobal
  szDefExt    db "lzss", 0
  szDefOutExt db "out", 0

  szUsage db "usage:", 13, 10, "  to compress: lzsspack input [output]", 13, 10
          db "  to decompress: lzsspack -u input output", 13, 10, 0

  szStatus db "status: ", 13, 10
           db "   total compression time: %d seconds", 13, 10
           db "            average speed: %d kbytes/sec", 13, 10
           db "       unpacked file size: %d bytes", 13, 10
           db "         packed data size: %d bytes", 13, 10
           db "        compression ratio: %d percent", 13, 10
           db 13, 10
           db "              total items: %d", 13, 10
           db "  direct (unpacked) bytes: %d", 13, 10
           db "       dictionary entries: %d", 13, 10, 0

endg


uglobal
  input_buf   dd ?
  input_size  dd ?
  output_name dd ?
  tmp         dd ?
  unp_size    dd ?
  argc        dd ?
  argv        dd ?
  env         dd ?
  hdr         LZSSFileHeader
  name_buf    rb 0x100
endg

;data fixups
;end data

section '.data' data readable writeable
IncludeAllGlobals


section '.idata' import data readable

library kernel32,"KERNEL32.DLL",\
        crtdll,"CRTDLL.DLL"

include "%finc%/win32/apia/kernel32.inc"

import crtdll,\
       __getmainargs,"__GetMainArgs",\
       printf,"printf",\
       flushall,"_flushall"